{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 问题： 我们相对字符串中的文本做查找和替换\n",
    "# 方案： str.replace re模块的sub()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yep, but no, but yep, but no, but yep\n"
     ]
    }
   ],
   "source": [
    "text = 'yeah, but no, but yeah, but no, but yeah'\n",
    "replaced_text= text.replace('yeah', 'yep')\n",
    "print(replaced_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today is 2012-11-27. PyCon starts 2013-3-13.\n"
     ]
    }
   ],
   "source": [
    "# 针对更为复杂的模式， 可以使用re模块中的sub()函数方法。\n",
    "# 如：把日期格式从 '11/27/2012' 改写为‘2012-11-27’\n",
    "# sub() 的第一个参数是要匹配的模式，第二个参数是要替换上的模式。\n",
    "import re\n",
    "text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'\n",
    "replaced_text = re.sub(r'(\\d+)/(\\d+)/(\\d+)',r'\\3-\\1-\\2',text)\n",
    "print(replaced_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today is 2012-11-27. PyCon starts 2013-3-13.\n"
     ]
    }
   ],
   "source": [
    "# 如果打算用相同的模式执行重复替换，可以考虑将模式编译以获得最好的性能。\n",
    "import re\n",
    "datepat = re.compile(r'(\\d+)/(\\d+)/(\\d+)')\n",
    "replaced_text = datepat.sub(r'\\3-\\1-\\2',text)\n",
    "print(replaced_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today is 27 Nov 2012. PyCon starts 13 Mar 2013.\n"
     ]
    }
   ],
   "source": [
    "# 对于更加复杂的情况，可以指定一个替换回调函数。\n",
    "from calendar import month_abbr\n",
    "\n",
    "\n",
    "def change_date(m):\n",
    "    # 将数字转换为Nov.月份简写。\n",
    "    mon_name = month_abbr[int(m.group(1))]\n",
    "    return '{} {} {}'.format(m.group(2), mon_name, m.group(3))\n",
    "text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'\n",
    "datepat = re.compile(r'(\\d+)/(\\d+)/(\\d+)')\n",
    "dd = datepat.sub(change_date, text)\n",
    "print(dd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 除了得到替换后的文本外，如果想知道一共替换了多少次，可以使用re.subn()。\n",
    "\n",
    "newtext, n = datepat.subn(r'\\3-\\1-\\2',text)\n",
    "print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}